package com.sybkl.bigdatastudentmybatisplus.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sybkl.bigdatastudentmybatisplus.controller.dto.UserDTO;
import com.sybkl.bigdatastudentmybatisplus.entity.User;
import com.sybkl.bigdatastudentmybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

//    @Autowired
//    private UserMapper userMapper;

    @Autowired
    private UserService userService;


//    @PostMapping("/login")
//    public boolean loginSuccess(@RequestParam String username, @RequestParam String password) {
//        User user = userMapper.selectByUsernameAndPassword(username, password);
//        if (ObjectUtils.isEmpty(user)) {
//            return false;
//        } else {
//            return true;
//        }
//    }

    //获取个人表单信息
    @GetMapping("/{username}")
    public List<User> getUserInfo(@PathVariable String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userService.list(queryWrapper);
    }

    @PostMapping("/login")
    public boolean login(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
//            return Result.error(Constants.CODE_400, "参数错误" );
            return false;
        }

//        return Result.error(Constants.CODE_200, "成功" );
        return userService.login(userDTO);
    }

    // 测试登录，浏览器访问： http://localhost:10029/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:10029/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }


    //修改用户个人信息
//    @PostMapping
//    public boolean updateUser(@RequestBody UserInfo userInfo){
//        String username = userInfo.getUsername();
//        if (StringUtils.isBlank(username)){
//            return false;
//        }
//
//        return ;
//    }
}
